package cn.sccl.dirms.monitor.controller;

import cn.sccl.common.zabbix.Zabbix;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 主机群主
 * Created by liupan on 2017/3/6.
 */
@RestController
@RequestMapping(value = "/dirms/monitor/group")
public class HostGroupController {


    @RequestMapping(value = "/all", method = {RequestMethod.POST,RequestMethod.GET})
    public List<Map<String,Object>> listHostgroup() {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("output","extend");
        params.put("with_hosts_and_templates",true);
        params.put("selectTemplates","extend");//选择所有的模板
        Map<String, Object> result = zabbix.call("hostgroup.get",params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        List<Map<String, Object>> reltList = new ArrayList<>();
        for (Map<String, Object> item : mapList) {
            if (!((String)item.get("name")).equals("Templates")) {
                item.put("id", item.get("groupid"));
                item.put("type", 1);
                reltList.add(item);
            }
        }
        return reltList;
    }
    //获取 有模板的 主机群组
    @RequestMapping(value = "/all/template/group", method = {RequestMethod.GET})
    public List<Map<String,Object>> listTemplatesGroup() {
        Zabbix zabbix = Zabbix.getZabbix();

        Map<String, Object> params = new HashMap<String,Object>();
        params.put("output","extend");
        params.put("templated_hosts",true);
        params.put("selectTemplates","extend");//选择所有的模板
        Map<String, Object> result = zabbix.call("hostgroup.get",params);

        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        List<Map<String, Object>> reltList = new ArrayList<>();
        for (Map<String, Object> item : mapList) {
            if (!((String)item.get("name")).equals("Templates")) {
                item.put("id", item.get("groupid"));
                item.put("type", 1);
                reltList.add(item);
            }
        }
        return reltList;
    }


    /**
     * 获取到所有组，用于设备列表树形结构展示
     */
    @RequestMapping(value = "/list", method = {RequestMethod.POST,RequestMethod.GET})
    public List<Map<String,Object>> listGroup() {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String, Object> params = new HashMap<String,Object>();
        params.put("output","extend");
        params.put("with_hosts_and_templates",true);
        Map<String, Object> result = zabbix.call("hostgroup.get",params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        List<Map<String, Object>> reltList = new ArrayList<>();
        for (Map<String, Object> item : mapList) {
            String groupName = (String)item.get("name");
            if (!groupName.equals("Templates") &&
                    !groupName.equals("Discovered hosts") &&
                    !groupName.equals("测试")) {
                item.put("id", item.get("groupid"));
                item.put("state", "closed");
                item.put("type", 1);
                item.put("children", new ArrayList<>());
                reltList.add(item);
            }
        }
        return reltList;
    }

    /**
     * 获取模板组
     */
    @RequestMapping(value = "/templatelist", method = {RequestMethod.POST,RequestMethod.GET})
    public List<Map<String,Object>> listTemplateGroup() {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String, Object> params = new HashMap<String,Object>();
        params.put("output","extend");
        params.put("templated_hosts",true);
        JSONObject temp = zabbix.call("hostgroup.get",params);
        Map<String, Object> root = new HashMap<String, Object>();
        root.put("groupid","-99");
        root.put("name","所有");
        root.put("selected",true);
        List<Map<String, Object>> result = (List<Map<String, Object>>) temp.get("result");
        result.add(0, root);
        return result;
    }

    /**
     * 根据名称查询主机群组
     * @param name 组名称
     */
    @RequestMapping(value = "/query/{name}", method = {RequestMethod.POST,RequestMethod.GET})
    public Map<String,Object> getGroupByName(@PathVariable String name) {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String, Object> params = new HashMap<String,Object>();
        params.put("output","extend");
        Map<String, Object> params2 = new HashMap<String, Object>();
        List<String> filters = new ArrayList<String>();
        filters.add(name);
        params2.put("name",filters);
        params.put("filter",params2);
        return zabbix.call("hostgroup.get",params);
    }

    /**
     * 获取包含host的所有组信息
     */
    @RequestMapping(value = "/selectHosts", method = {RequestMethod.POST,RequestMethod.GET})
    public Map<String,Object> selectHosts() {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String, Object> params = new HashMap<String,Object>();
        params.put("output","extend");
        params.put("selectHosts","extend"); // 选择
//        params.put("selectInventory","extend"); // 主机资产记录
        return zabbix.call("hostgroup.get",params);
    }


    /**
     * 批量删除图形信息
     * @return
     */
    @RequestMapping(value = "/delete/ids", method = {RequestMethod.POST, RequestMethod.GET})
    public List<String> deleteByIds(@RequestParam("ids") String ids){
        Zabbix zabbix = Zabbix.getZabbix();
        if(ids != null && ids.length() > 0) {
            String[] idArr = ids.trim().split(",");
            Map<String, Object> resp = zabbix.call("hostgroup.delete", Arrays.asList(idArr));
            Map<String, Object> result = (Map<String, Object>) resp.get("result");
            List<String> res = (List<String>) result.get("groupids");
            return res;
        }
        return null;
    }
}
